# You're Making Us Look Bad: Can Partisan Embarrassment Dampen Partisanship and Polarization?
# Elizabeth C. Connors, Taylor N. Carlson, and Steven W. Webster
# Study 1 Hand-Coded Analysis (Figure 1) Replication Code
# July 10, 2025

# Install packages if needed: install.packages("haven")

# Load Packages
library(haven)

# --- For background --- #

# Set your working directory to where you have study1_handcoded.csv stored

setwd()

# Load data 
d <- read.csv("study1_handcoded.csv")

# --- For Analysis of Scenario Descriptions --- # 

# Note: (see codebook for more details)
# ucc108_treat takes two values reflecting which question version respondents received initially
# UCC108 = ELites: "Have you ever felt embarrassed to be a [Republican / Democrat] because of something a [Republican / Democratic] elite (i.e., a politician or media elite) said or did?"
# UCC109 = Peers: "Have you ever felt embarrassed to be a [Republican / Democrat] because of something a [Republican / Democratic] peer (i.e., not a [Republican / Democratic] elite but a [Republican / Democrat] in the public) said or did?"

# How many responses to code? 
table(d$relevant)

# How many experienced partisan embarrassment and were therefore asked the question? 
table(d$ucc108)
# every week 10
# once or twice 88
# quite often 27
# sometimes 96
# no never 175
length(which(d$ucc108 != "No, never"))

table(d$ucc109)
# every week 6
# once or twice 99
# quite often 22 
# sometimes 91
# no, never 177 
length(which(d$ucc109 != "No, never"))

## Sum together elites vs. peers 
length(which(d$ucc109 != "No, never")) + length(which(d$ucc108 != "No, never"))

# Proportion "Other" 
prop.table(table(d$other)) # .596 

# --- Figure 1 --- #



# Proportions of each category overall and by treatment (elite vs. peer)
cat <- matrix(NA, nrow = 5, ncol = 3)
cat[1,] <- c(prop.table(table(d$sex_scandal))[2], # overall
             prop.table(table(d$sex_scandal[d$ucc108_treat == "UCC108"]))[2], # elites
             prop.table(table(d$sex_scandal[d$ucc108_treat == "UCC109"]))[2]) # peer
cat[2,] <- c(prop.table(table(d$too_extreme))[2], # overall
             prop.table(table(d$too_extreme[d$ucc108_treat == "UCC108"]))[2], # elites
             prop.table(table(d$too_extreme[d$ucc108_treat == "UCC109"]))[2]) # peer
cat[3,] <- c(prop.table(table(d$too_moderate))[2], # overall
             prop.table(table(d$too_moderate[d$ucc108_treat == "UCC108"]))[2], # elites
             prop.table(table(d$too_moderate[d$ucc108_treat == "UCC109"]))[2]) # peer
cat[4,] <- c(prop.table(table(d$misinformation))[2], # overall
             prop.table(table(d$misinformation[d$ucc108_treat == "UCC108"]))[2], # elites
             prop.table(table(d$misinformation[d$ucc108_treat == "UCC109"]))[2]) # peer
cat[5,] <- c(prop.table(table(d$uninformed))[2], # overall
             prop.table(table(d$uninformed[d$ucc108_treat == "UCC108"]))[2], # elites
             prop.table(table(d$uninformed[d$ucc108_treat == "UCC109"]))[2]) # peer

rownames(cat) <- c("Sex Scandal", "Too Extreme", "Too Moderate", "Misinformed", "Uninformed")
colnames(cat) <- c("Full Sample", "Elites", "Peers")

cat

# Get SEs
## Create table
cat_SE <- matrix(NA, nrow = 5, ncol = 2)

## Store proportions (p) and number of observations (n)
sex_scandal_pe <- cat[1,2]
sex_scandal_pp <- cat[1,3]
n_e <- sum(table(d$sex_scandal, d$ucc108_treat)[,1])
n_p <- sum(table(d$sex_scandal, d$ucc108_treat)[,2])

too_extreme_pe <- cat[2,2]
too_extreme_pp <- cat[2,3]
n_e <- sum(table(d$too_extreme, d$ucc108_treat)[,1])
n_p <- sum(table(d$too_extreme, d$ucc108_treat)[,2])

too_moderate_pe <- cat[3,2]
too_moderate_pp <- cat[3,3]
n_e <- sum(table(d$too_moderate, d$ucc108_treat)[,1])
n_p <- sum(table(d$too_moderate, d$ucc108_treat)[,2])

misinformation_pe <- cat[4,2]
misinformation_pp <- cat[4,3]
n_e <- sum(table(d$misinformation, d$ucc108_treat)[,1])
n_p <- sum(table(d$misinformation, d$ucc108_treat)[,2])

uninformed_pe <- cat[5,2]
uninformed_pp <- cat[5,3]
n_e <- sum(table(d$uninformed, d$ucc108_treat)[,1])
n_p <- sum(table(d$uninformed, d$ucc108_treat)[,2])



## Calculate and place in table sqrt(p*(1-p)/n)
cat_SE[1,] <- c(sqrt(sex_scandal_pe * (1-sex_scandal_pe)/n_e),
                sqrt(sex_scandal_pp * (1-sex_scandal_pp)/n_p))
cat_SE[2,] <- c(sqrt(too_extreme_pe * (1-too_extreme_pe)/n_e),
                sqrt(too_extreme_pp * (1-too_extreme_pp)/n_p))
cat_SE[3,] <- c(sqrt(too_moderate_pe * (1-too_moderate_pe)/n_e),
                sqrt(too_moderate_pp * (1-too_moderate_pp)/n_p))
cat_SE[4,] <- c(sqrt(misinformation_pe * (1-misinformation_pe)/n_e),
                sqrt(misinformation_pp * (1-misinformation_pp)/n_p))
cat_SE[5,] <- c(sqrt(uninformed_pe * (1-uninformed_pe)/n_e),
                sqrt(uninformed_pp * (1-uninformed_pp)/n_p))

# Prepare to save figure
png("figure1.png",width=6,height=6,units="in",res=600)

barCenters <- barplot(cat[, 2:3], beside = T, 
                      main = "Types of Embarrassing Scenarios",
                      ylim=c(0,.5), 
                      col = c("gray0", "gray40", "gray60", "gray80", "gray100"),
                      ylab = "Proportion")
segments(barCenters, cat[,2:3] + 1.96*cat_SE, barCenters, cat[,2:3] - 1.96*cat_SE)
legend(0.5, .5, legend = c("Sex Scandal", "Too Extreme", "Too Moderate", "Misinformed", "Uninformed"),
       fill = c("gray0", "gray40", "gray60", "gray80", "gray100"), bty="n")

dev.off()

# Report statistics 
cat
